package model

import (
	"background/db"
	"background/logs"
	"fmt"
	"log"
)

type File struct {
	ID      int64  `sql:"id" json:"id"`
	Filename   string `sql:"filename" json:"filename"`
	Size    int64  `sql:"size" json:"size"`
	Types  string `sql:"types"  json:"types"`
}

func GetFiles()  ([]File,error){
	ret := []File{}
	sql := fmt.Sprintf("select * from files")
	e := db.GetBlogMysql().Query2(sql, &ret)
	log.Print(ret)
	if nil != e {
		logs.Error(e.Error())
		return nil,e
	}
	return ret,nil
}

func DeleteFiles(id int)  error{
	sql := fmt.Sprintf("delete from files where id = '%d'",id)
	_,e := db.GetBlogMysql().Query(sql)
	if nil != e {
		logs.Error(e.Error())
		return e
	}
	return nil
}

func AddFile(f File)  error{
	sql := fmt.Sprintf("insert into files(filename,size,types) values(%s,%d,%d)",f.Filename,
		f.Size,f.Types)
	var ret map[string]interface{}
	e := db.GetBlogMysql().Query2(sql, &ret)
	if nil != e{
		logs.Error(e.Error())
		return e
	}
	return nil
}